﻿using SmartPatientCentre.Common;
using SmartPatientCentre.Models;
using SmartPatientCentre.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace SmartPatientCentre.Pages
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Login : Page
    {
        public Login()
        {
            this.InitializeComponent();
        }

        private void LoginButtonClick(object sender, RoutedEventArgs e)
        {
            LogInUser();
        }

        ////// Error : Access is denied??!!
        //private void LoginKeyDown(object sender, KeyRoutedEventArgs e)
        //{
        //    if (e.Key == VirtualKey.Enter)
        //    {
        //        LogInUser();
        //    }
        //}

        private async void LogInUser()
        {
            // Display Progress Bar and Disable Login Button
            LoginProgressRing.IsActive = true;
            LoginButton.IsEnabled = false;

            // Get user input
            string username = UsernameTextBox.Text;
            string password = LoginPasswordBox.Password;
            string message = null;

            //// Username and password cannot be empty
            //if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            //{
            //    message = "Username and password cannot be empty. Please try again";
            //    await new MessageDialog(message).ShowAsync();

            //    // Collapse Progress Ring and Enable Login Button
            //    LoginProgressRing.IsActive = false;
            //    LoginButton.IsEnabled = true;

            //    return;
            //}

            // Start Communication with Web API
            DownloadClient client = new DownloadClient();

            try
            {
                // Username and password cannot be empty
                if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
                {
                    throw new Exception("Username and password cannot be empty. Please try again");
                }

                // Authenticate
                Status.Text = "Authenticating user...";
                App.Data.User = await client.AuthenticateUser(username, password);

                // Get Patient List
                Status.Text = "Downloading patient list...";
                //App.Data.PatientList = await client.GetPatients(App.Data.User);
                //App.Data.PatientList = await client.GetPatientsByUser(App.Data.User);
                App.Data = await client.GetDataByUser(App.Data.User);
            }
            catch(Exception ex)
            {
                message = ex.Message;
            }

            // Exception Message Dialog
            if (!string.IsNullOrWhiteSpace(message))
            {
                await new MessageDialog(message).ShowAsync();

                // Collapse Progress Ring and Enable Login Button
                LoginProgressRing.IsActive = false;
                LoginButton.IsEnabled = true;
                Status.Text = "";
            }
            else
            {
                // Navigate to Dashboard if successfully logged in
                Frame.Navigate(typeof(Pages.Dashboard));
            }
        }
    }
}
